﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity.Framework;
using System.Data.Objects;

namespace Business.Operations
{
    public class MacOperation
    {
        private static HPDMASEntities db = new HPDMASEntities();

        public MacOperation()
        {

        }

        public static List<Mac> MacGetAll()
        {
            var macListesi = db.Mac.ToList();
            return macListesi;
        }

        public static Mac MacGetByMacId(int macId)
        {
            var mac = (from c in db.Mac where c.MacID == macId select c).First();
            return mac;
        }

        public static void MacDelete(int macId)
        {
            Mac k = (from c in db.Mac where c.MacID == macId select c).First();
            db.DeleteObject(k);
            db.SaveChanges();
        }

        public static int MacSayisiGetBySehirId(int sehirId)
        {
            var macSayisi = (from c in db.Mac where c.SehirID == sehirId select c.MacID).Count();
            return macSayisi;
        }

        /// <summary>
        /// Using Entity Framework (with Linq To Sql query)
        /// </summary>
        /// <param name="sehirId"></param>
        /// <returns></returns>
        public static List<Mac> MacGetBySehirId(int sehirId)
        {
            //var macList = db.Mac.
            //    Where(c => c.SehirID == sehirId).
            //    Select(c => new 
            //    {
            //        MacKodu = c.MacKodu,
            //        EvSahibiTakim = c.EvSahibiTakim.TakimAdi,
            //        MisafirTakim = c.MisafirTakim.TakimAdi,
            //        Grup = c.MacGrup.MacGrupAdi,
            //        Hafta = c.MacHafta,
            //        Tarih = c.MacTarihi,
            //        Saat = c.MacSaati,
            //        Stat = c.Stat.StatAdi,
            //        Ilce = c.Ilce.IlceAdi
            //    });

            //List<object> list = new List<object>(macList.ToList());
            var list = db.Mac.Where(c => c.SehirID == sehirId).Select(c => c).ToList();
            return list;
        }

        public static void MacInsert(Mac mac)
        {
            db.Mac.AddObject(mac);
            db.SaveChanges();
        }

        public static void MacUpdate(Mac m)
        {
            db.Mac.ApplyCurrentValues(m);
            db.SaveChanges();
        }

        public static List<string> MacAdiGetByLigId(int ligId)
        {
            var macAdiListesi = db.Mac.
                Where(c => c.LigID == ligId).
                Select(c => c.EvSahibiTakim.TakimAdi + " - " + c.MisafirTakim.TakimAdi).ToList();
            return macAdiListesi;
        }

        public static int MacIdGetByMacAdi(string macAdi)
        {
            var macId = db.Mac.
                Where(c => c.EvSahibiTakim.TakimAdi + " - " + c.MisafirTakim.TakimAdi == macAdi).Select(c => c.MacID).First();
            return macId;
        }

        public static string MacSaatiGetByMacId(int macId)
        {
            string macSaati = db.Mac.
                Where(c => c.MacID == macId).
                Select(c => c.MacSaati).First().
                ToString();
            return macSaati;
        }

        public static string MacGunuGetByMacId(int macId)
        {
            string macGunu = db.Mac.
                Where(c => c.MacID == macId).
                Select(c => c.MacTarihi).First().
                ToString("dddd");
            return macGunu;
        }

        public static string MacTarihiGetByMacId(int macId)
        {
            string macTarihi = db.Mac.
                Where(c => c.MacID == macId).
                Select(c => c.MacTarihi).First().
                ToString("dd.MM.yyyy");
            return macTarihi;
        }

        public static string MacSahaTipiGetByMacId(int macId)
        {
            string saha = db.Mac.
                Where(c => c.MacID == macId).
                Select(c => c.MacSahaTipi).First();
            return saha;
        }

        public static string MacKategoriGetByMacId(int macId)
        {
            string kategori = db.Mac.
                Where(c => c.MacID == macId).
                Select(c => c.Lig.LigAdi).First();
            return kategori;
        }

        public static List<object> MacGetAllGetBySehirId(int sehirId)
        {
            var macList = db.Mac.
                Where(c => c.SehirID == sehirId).
                OrderByDescending(c => c.MacTarihi).
                Select(c => new
                {
                    MaçKodu = c.MacKodu,
                    EvSahibiTakım = c.EvSahibiTakim.TakimAdi,
                    MisafirTakım = c.MisafirTakim.TakimAdi,
                    Tarih = c.MacTarihi,
                    Gün = c.MacTarihi,
                    Saat = c.MacSaati,
                    Hafta = c.MacHafta,
                    Stad = c.Stat.StatAdi,
                    İlçe = c.Ilce.IlceAdi
                });

            List<object> list = new List<object>(macList.ToList());
            return list;
        }

        public static string IlceAdiGetByMacId(int macId)
        {
            var ilceAdi = db.Mac.
                Where(c => c.MacID == macId).
                Select(c => c.Ilce.IlceAdi).FirstOrDefault();
            return ilceAdi;
        }

        public static string StatAdiGetByMacId(int macId)
        {
            var statAdi = db.Mac.
                Where(c => c.MacID == macId).
                Select(c => c.Stat.StatAdi).FirstOrDefault();
            return statAdi;
        }

        public static int IlceIdGetByMacId(int macId)
        {
            var ilceId = db.Mac.
                Where(c => c.MacID == macId).
                Select(c => c.Ilce.IlceID).First();
            return ilceId;
        }

        public static string LigAdiGetByMacId(int macId)
        {
            var ligAdi = db.Mac.
                Where(c => c.MacID == macId).
                Select(c => c.Lig.LigAdi).FirstOrDefault();
            return ligAdi;
        }

        public static string MacGrupAdiGetByMacId(int macId)
        {
            var grupAdi = db.Mac.
                Where(c => c.MacID == macId).
                Select(c => c.MacGrup.MacGrupAdi).FirstOrDefault();
            return grupAdi;
        }

        public static string MacHaftaGetByMacId(int macId)
        {
            var hafta = db.Mac.
                Where(c => c.MacID == macId).
                Select(c => c.MacHafta).First();
            return hafta.ToString();
        }

        public static string EvSahibiTakimAdiGetByMacId(int macId)
        {
            var evSahibiTakimAdi = db.Mac.
                Where(c => c.MacID == macId).
                Select(c => c.EvSahibiTakim.TakimAdi).First();
            return evSahibiTakimAdi;
        }

        public static string MisafirTakimAdiGetByMacId(int macId)
        {
            var misafirTakimAdi = db.Mac.
                Where(c => c.MacID == macId).
                Select(c => c.MisafirTakim.TakimAdi).First();
            return misafirTakimAdi;
        }

        public static string DevreGetByMacId(int macId)
        {
            var devreNo = db.Mac.
                Where(c => c.MacID == macId).
                Select(c => c.Devre.DevreNo).First();
            return devreNo.ToString();
        }

        public static int LigIdGetByMacId(int macId)
        {
            var ligId = db.Mac.
                Where(c => c.MacID == macId).
                Select(c => c.LigID).First();
            return (Int32)ligId;
        }
    }
}